// $Id: main.cpp 46 2004-09-29 11:43:36Z jonas $
#include <stdlib.h>
#include "loghandler.h"
#include "eos.h"
#include <iostream>
using namespace std;

/** \mainpage Eos Index Page
 *
 * \section intro_sec Introduction
 *
 * This is the introduction.
 *
 * \section install_sec Installation
 *
 * \subsection step1 Step 1: Opening the box
 *
 * etc...
 *
 * (This text is located in main.cpp)
 *
 * @todo Write the index page
 */

int main(int argc, char **argv)
{
	char scenario[64] = {0};
	unsigned int seed = 0;
	unsigned int iterationCount = 0;
	unsigned int nodeCount = 0;
	int fileCount = -1;
	unsigned int nodeFileStoreLimit = 0;
	unsigned int nodeRoutingTableLimit = 0;
	unsigned int requestHopsToLive = 0;
	unsigned int insertHopsToLive = 0;
	float requestProbability = -1;
	float insertProbability = -1;

	// Read parameters
	for(int i=1; argc > i; i++)
	{

		// Arguments w/o parameters
		if(!strncmp(argv[i], "-h", 2) || !strncmp(argv[i], "--help", 2)){
			/// @todo Write a decent help....
			cout << "Use the source mook." << endl;
			exit(1);
		}

		// Arguments w. one parameter
		if(argc > i+1){
			if(!strcmp(argv[i], "-s") || !strcmp(argv[i], "--scenario")){
				strncpy(scenario, argv[++i], 64);
			}
			else if(!strcmp(argv[i], "-r") || !strcmp(argv[i], "--randomseed")){
				seed = atoi(argv[++i]);
			}
			else if(!strcmp(argv[i], "-d") || !strcmp(argv[i], "--duration")){
				iterationCount = atoi(argv[++i]);
			}
			else if(!strcmp(argv[i], "-n") || !strcmp(argv[i], "--networksize")){
				nodeCount = atoi(argv[++i]);
			}
			else if(!strcmp(argv[i], "-f") || !strcmp(argv[i], "--numberoffiles")){
				fileCount = atoi(argv[++i]);
			}
			else if(!strcmp(argv[i], "-fs") || !strcmp(argv[i], "--filestoresize")){
				nodeFileStoreLimit = atoi(argv[++i]);
			}
			else if(!strcmp(argv[i], "-ts") || !strcmp(argv[i], "--routingtablesize")){
				nodeRoutingTableLimit = atoi(argv[++i]);
			}
			else if(!strcmp(argv[i], "-rhtl") || !strcmp(argv[i], "--requesthopstolive")){
				requestHopsToLive = atoi(argv[++i]);
			}
			else if(!strcmp(argv[i], "-ihtl") || !strcmp(argv[i], "--inserthopstolive")){
				insertHopsToLive = atoi(argv[++i]);
			}
			else if(!strcmp(argv[i], "-rp") || !strcmp(argv[i], "--requestprobability")){
				requestProbability = atof(argv[++i]);
			}
			else if(!strcmp(argv[i], "-ip") || !strcmp(argv[i], "--insertprobability")){
				insertProbability = atof(argv[++i]);
			}
			else {
				// No such argument!
				cout << "No such argument: \"" << argv[i] << "\". Panic." << endl;
				exit(1);
			}
		}
		else {
			// No such argument!
			cout << "All parameters take parameters! Panic." << endl;
			exit(1);
		}

	}

	// Check parameters
	if(!scenario[0]){
		cout << "No scenario choosen." << endl;
		exit(1);
	}
	if(!seed){
		cout << "No random seed chosen. Creating one using local time: ";
		seed = time(0);
		cout << seed << endl;
	}

	// Seed the pseudo random generator
	srand(seed);
	LogHandler::getInstance()->main_seed(seed);

	// Initialize and run the simulation
	Eos eos(scenario, seed, iterationCount, nodeCount, fileCount, nodeFileStoreLimit, nodeRoutingTableLimit, requestHopsToLive, insertHopsToLive, requestProbability, insertProbability);
	eos.init();
	eos.simulate();

	// Clean up and exit
	LogHandler::cleanUp();
	return 0;
}
